package com.cgsd.heyu.widgets.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.ClickItemListener;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.ShareAdapter;
import com.cgsd.heyu.common.CommonConfig;
import com.cgsd.heyu.entity.TabEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 分享弹窗
 * CreateData: 2020/11/3
 */
public class ShareDialog extends Dialog implements View.OnClickListener, ClickItemListener {
    /**
     * 草根时代详情
     */
    public static final int TYPE_DEARY_DETAIL = 1;
    /**
     * 分类列表页面
     */
    public static final int TYPE_CLASSIFY_LIST = 2;
    /**
     * 用户草根时代详情
     */
    public static final int TYPE_PERSONAL_LIST = 3;


    /**
     * 分享到微信
     */
    public static final String SHARE_WEIXIN = "share_weixin";
    /**
     * 分享到朋友圈
     */
    public static final String SHARE_FRIENDS = "share_friends";
    /**
     * 保存图片到本地相册
     */
    public static final String SAVE_IMAGE = "save_image";
    /**
     * 删除草根时代
     */
    public static final String DELETE_DIARY = "delete_diary";
    /**
     * 举报草根时代,举报分类，举报用户
     */
    public static final String REPORT_DIARY = "report";
    /**
     * 复制链接
     */
    public static final String COPY_URL = "copy_url";
    /**
     * 修改分类
     */
    public static final String UPDATE_CLASSIFY_DES = "update_classify_des";

    private LinearLayout layoutFlower;
    private TextView tvFlower;
    private TextView tvTitle;
    private Context mContext;
    private ShareAdapter adapter;
    private List<TabEntity> data = new ArrayList<>();
    private DialogListener dialogListener;

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public ShareDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom_bottom);
        this.mContext = context;
        setContentView(R.layout.dialog_share);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
        }
        setCanceledOnTouchOutside(true);
        Window dialogWindow = getWindow();
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels);
        dialogWindow.setAttributes(lp);
        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        layoutFlower = findViewById(R.id.layout_flower);
        tvFlower = findViewById(R.id.tv_flower);
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);


        adapter = new ShareAdapter(mContext,data);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false));
        mRecyclerView.setAdapter(adapter);
        adapter.setClickItemListener(this);

        findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvFlower.setText(CommonConfig.getInstance().publishReword +mContext.getString(R.string.a_flower));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_confirm){
            dismiss();
        }
    }

    /**
     * 设置数据
     * @param isSelf 是否是本人
     * @param title 标题
     * @param type 类型
     */
    public void setData(boolean isSelf,String title,int type){
        tvTitle.setText(title);
        data.clear();
        switch (type){
            case TYPE_DEARY_DETAIL:
                data.add(new TabEntity(mContext.getString(R.string.weixin_friend),R.mipmap.ic_share_wechat,SHARE_WEIXIN));
                data.add(new TabEntity(mContext.getString(R.string.friends),R.mipmap.ic_share_pengyq,SHARE_FRIENDS));
                data.add(new TabEntity(mContext.getString(R.string.share_url),R.mipmap.ic_share_url,COPY_URL));
                if (isSelf){
                    data.add(new TabEntity(mContext.getString(R.string.save_photo),R.mipmap.ic_share_download,SAVE_IMAGE));
                    data.add(new TabEntity(mContext.getString(R.string.delete_diary),R.mipmap.ic_share_delete,DELETE_DIARY));
                }else {
                    data.add(new TabEntity(mContext.getString(R.string.report_diary),R.mipmap.ic_share_report,REPORT_DIARY));
                }
                break;
            case TYPE_CLASSIFY_LIST:
                data.add(new TabEntity(mContext.getString(R.string.report_classify),R.mipmap.ic_share_report,REPORT_DIARY));
                data.add(new TabEntity(mContext.getString(R.string.update_classify_des),R.mipmap.ic_share_edit,UPDATE_CLASSIFY_DES));
                break;
            case TYPE_PERSONAL_LIST:
                data.add(new TabEntity(mContext.getString(R.string.report_user),R.mipmap.ic_share_report,REPORT_DIARY));
                break;
            default:
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 增加获取积分奖励标题
     * @param isShow 是否显示
     */
    @SuppressLint("SetTextI18n")
    public void isShowFlowerReword(boolean isShow){
        if(isShow) {
            layoutFlower.setVisibility(View.VISIBLE);
        }else {
            layoutFlower.setVisibility(View.GONE);
        }
    }

    @Override
    public void clickItem(View view, int position) {
        if (dialogListener != null){
            dialogListener.dialogClick(data.get(position));
        }
    }

    public interface DialogListener{
        void dialogClick(TabEntity tabEntity);
    }

    @Override
    public void show() {
        super.show();
    }

}
